package com.toivan.mt.fragment;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shizhefei.fragment.LazyFragment;
import com.toivan.mt.R;
import com.toivan.mt.adapter.MtAtmosphereAdapter;
import com.toivan.mt.model.MtAtmosphere;
import com.toivan.mt.utils.MtCallback;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.sdk.MtSDK;

import java.util.ArrayList;
import java.util.List;

public class MtAtmosphereFragment extends LazyFragment {

    private List<MtAtmosphere> list = new ArrayList<>();
    private MtSDK mtSDK;
    private MtAtmosphereAdapter adapter;

    public MtAtmosphereFragment setMtSDK(MtSDK mtSDK) {
        this.mtSDK = mtSDK;
        return this;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);

        setContentView(R.layout.fragment_sticker_recyclerview);

        if (getContext() == null) return;

        list.clear();
        List<MtAtmosphere> atmospheres = MtJsonParser.getInstance().getAllAtmospheres();
        if (atmospheres == null || atmospheres.size() == 0) {
            MtJsonParser.getInstance().getAtmospheres( new MtCallback<List<MtAtmosphere>>() {
                @Override
                public void callback(List<MtAtmosphere> bean) {
                    if (bean != null) {
                        list.addAll(bean);
                        showAtmosphere();
                    }
                }
            });
        } else {
            list.addAll(atmospheres);
            showAtmosphere();
        }
    }

    private void showAtmosphere() {
        RecyclerView atmosphereRV = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new MtAtmosphereAdapter(list);
        atmosphereRV.setLayoutManager(new GridLayoutManager(getContext(), 5));
        atmosphereRV.setAdapter(adapter);
    }

    public void cancelAtmosphere() {
        if (adapter != null) {
            adapter.cancelSelected();
        }
    }


}


